//
//  BaseTableView.m
//  HTP
//
//  Created by admin on 15/10/26.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "BaseTableView.h"

@implementation BaseTableView
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style withHeadRefresh:(BOOL)isHead andFoodRefresh:(BOOL)isFoot{
    self = [super initWithFrame:frame style:style];
    if (self) {
        _isNeedWhiteBackground = NO;
        if (isHead) {
            self.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headRefresh)];
        }
        if (isFoot) {
            self.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footRefresh)];
        }
        [self setExtraCellLineHidden];
    }
    return self;
}

//TODO:下拉刷新
-(void)headRefresh{
    if (self.baseTableDelegate) {
        [self.baseTableDelegate headRefresh:self];
    }
    [self performSelector:@selector(endHeadRefresh) withObject:nil afterDelay:2.0];
}
-(void)endHeadRefresh{
    switch (self.mj_header.state) {
        case MJRefreshStateRefreshing:
            [self.mj_header endRefreshing];
            break;
        default:
            break;
    }
}
//TODO:上拉动作
-(void)footRefresh{
    if (self.baseTableDelegate) {
        [self.baseTableDelegate footRefresh:self];
    }
    [self performSelector:@selector(endFootRefresh) withObject:nil afterDelay:10.0];
}
-(void)endFootRefresh{
    switch (self.mj_footer.state) {
        case MJRefreshStateRefreshing:
            [self.mj_footer endRefreshing];
            break;
        default:
            break;
    }
}
-(void)reloadData{
    [super reloadData];
    if (_isNeedWhiteBackground) {
        if (self.indexPathsForVisibleRows.count == 0) {
            CGFloat size = self.frame.size.width > self.frame.size.height?self.frame.size.height:self.frame.size.width;
            UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size, size)];
            
            UIImageView *noneImg = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width - size)/2.0, 5, size , size - 25)];
            noneImg.contentMode = UIViewContentModeScaleAspectFit;
            noneImg.image = [UIImage imageNamed:@"nullPeople"];
            [footView addSubview:noneImg];
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width - size)/2.0, size - 20, size , 20)];
            image.contentMode = UIViewContentModeScaleAspectFit;
            image.image = [UIImage imageNamed:@"nullBg"];
            [footView addSubview:image];
            self.tableFooterView = footView;
        }else{
            self.tableFooterView = nil;
        }
    }
    
}
- (void)setExtraCellLineHidden{
    
    UIView *view =[ [UIView alloc]init];
    
    view.backgroundColor = [UIColor clearColor];
    
    [self setTableFooterView:view];
    
    [self setTableHeaderView:view];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
